// Trend variables
float plotX1, plotY1;
float plotX2, plotY2;

TrendTable[] data;
int dataMin, dataMax;
color[] colJ = new color[7];

int Cur_month=6;

int T_hourMin=0, T_hourMax=23;

// this variable stores the text that gets drawn when the user
// hovers over a particular area in the graph
String trendText;

// string to keep description information for trends display type
static final String trendsInfo = "TRENDS: The trends display type\n"
             +"shows hashtag information over\n"
             +"a 24 hour period, over the 7\n"
             +"months of twitter data provided.\n\n"
             +"TO GET STARTED: Select a month\nfrom "
             +"the drop down list and hover\nover the "
             +"different hour sections to \nsee hash "
             +"information in the box \nabove.";

void setupTrends() {
  int i;
  data = new TrendTable[7];
  for(i=0; i<7; i++)
    data[i] = new TrendTable("top_5_" + (i+6));
  plotX1 = 190;
  plotX2 = width - 25;
  plotY1 = 10; 
  plotY2 = height - 25;
  smooth();

  colJ[0]  = color(237, 109,   0, 180); 
  colJ[1]  = color(  0,  98, 150, 180); 
  colJ[2]  = color(221,  56,  28, 180);
  colJ[3]  = color(105, 178, 115, 180); 
  colJ[4]  = color(200, 146,  45, 180); 
  colJ[5]  = color(185, 101, 177, 180);
  colJ[6]  = color(247, 186, 129, 180);
}

void drawTrends() {
  background(255);
  fill(255);
  rectMode(CORNERS);
  noStroke();
  rect(plotX1, plotY1, plotX2, plotY2);
  drawX();
  drawY();
  setMinMax();
  strokeWeight(5);
  stroke(#5679C1);
  drawPoints(Cur_month);
  showDescriptionBox(trendsInfo);
}

void setMinMax() {
  int maxi=data[0].getCountMax();
  int mini = data[0].getCountMin();
  for(int i=1; i<7; i++) {
    if(maxi<data[i].getCountMax())
      maxi = data[i].getCountMax();
    if(mini > data[i].getCountMin())
      mini = data[i].getCountMin();
  }
  dataMax = maxi;
  dataMin = mini;
}

void drawY() {
  int i;
  for(i=dataMin; i<=dataMax; i+=4500) {
    noFill();
    stroke(200);
    strokeWeight(1);
    float yLine = map(i, dataMin, dataMax, plotY2-50, plotY1+20);
    line(plotX1, yLine, plotX1+10, yLine);
    fill(120);
    noStroke();
    textAlign(RIGHT);
    textSize(10);
    float yText = map(i, dataMin, dataMax, plotY2-50, plotY1+30);
    text(i, plotX1+40, yText);
  }
}
void drawX() {
  int i;
  float[] xlinePos = new float[24];
  trendText = "";

  for(i=T_hourMin; i<=T_hourMax; i++) {
    if(T_hourMin == T_hourMax) {
      fill(120);
      noStroke();
      textAlign(CENTER);
      textSize(10);
      text(i, (plotX1+plotX2+30)/2, plotY2-30);
    }
    else {     
      noFill();
      stroke(200);
      strokeWeight(1);
      float xLine = map(i, T_hourMin, T_hourMax, plotX1+50, plotX2-20);
      line(xLine, plotY1+20, xLine, plotY2-50);
      xlinePos[i] = xLine;
      fill(120);
      noStroke();
      textAlign(CENTER);
      textSize(10);
      float xText = map(i, T_hourMin, T_hourMax, plotX1+55, plotX2-25);
      text(i, xText, plotY2-30);
    }
  }
  for(i=T_hourMin; i<=T_hourMax-1; i++) {
    if(mouseX>=xlinePos[i] && mouseX<xlinePos[i+1]) {
      if(mouseY<=plotY2-30 && mouseY>=plotY1+20) {
        // create the text string that will be displayed when the user
        // hovers over a time section
        trendText += "Number of Tweets:";

        int count=0;
        for(int j=0;j<5;j++)
          count+=data[Cur_month-6].hourlyCount[i*5+j][1];

        trendText += count + "\n";
        trendText += "Top 5 Topics\n";
        trendText += "from " + i + " to " + (i+1)+ ":\n";
        trendText += data[Cur_month-6].topTopic[i*5] + "\n";
        trendText += data[Cur_month-6].topTopic[i*5+1] + "\n";
        trendText += data[Cur_month-6].topTopic[i*5+2] + "\n";
        trendText += data[Cur_month-6].topTopic[i*5+3] + "\n";
        trendText += data[Cur_month-6].topTopic[i*5+4] + "\n";
        showInfoBox(trendText);
      }
    }
  }
}


void drawPoints(int Month) {
  strokeWeight(1);
  stroke(220);
  fill(255);

  int count, index=0;

  fill(colJ[Month-6]);
  beginShape();
  vertex(plotX1+50, plotY2-50);
  for(int i=T_hourMin; i<=T_hourMax; i++) {
    count = 0;
    for(int j=0; j<5; j++) {
      count += data[Month-6].hourlyCount[i*5+j][1];
    }
    float x = map(i, T_hourMin, T_hourMax, plotX1+50, plotX2-20);
    float y = map(count, dataMin, dataMax, plotY2-50, plotY1);

    vertex(x,y);
  }
  vertex(plotX2-20, plotY2-50);
  endShape();
}


